//青蛙管理类
//1.生成青蛙缓存池子
//2.给所有的青蛙设置目标点
//3.更新所有的青蛙


class frogMgr extends eui.Component {
	public constructor(game: any) {
		super();
		this.gameHandle = game;
		this.displayContainer = this.gameHandle.gameFrog;
	}


	/**玩家类型 */
	private clothes_List = ["lv", "huang", "qing"];

	private gameHandle;
	/**本地地图上所有青蛙 */
	private frogListByClient: control.frog[] = [];
	/**服务器地图上所有青蛙 */
	private frogListByServer: eui.Image[] = [];
	/**显示容器  用于显示青蛙 */
	public displayContainer;
	private changeClothesMgr
	/**自己 */
	private myUser;
	/**判断一下青蛙最大的数量 */
	private maxFogNumbers = 20;

	private m_lineMgr;


	/**创建青蛙缓存 */
	public createAllFrogs() {
		this.frogListByClient = [];
		this.m_lineMgr = this.gameHandle.tongueManager;
		for (var i = 0; i < this.maxFogNumbers; i++) {
			var frog = new control.frog(this);
			frog.init(i);
			this.frogListByClient.push(frog);
		}
	}
	/**创建联网青蛙缓存 */
	public createAllNetFrogs() {
		//this.m_lineMgr = this.gameHandle.m_creatAllLine;
		this.frogListByClient = [];
		this.m_lineMgr = this.gameHandle.tongueManager;
		for (var i = 0; i < this.maxFogNumbers; i++) {
			var frog = new control.frogNet(this);
			frog.init(i);
			this.frogListByClient.push(frog);
		}
	}
	/**清楚所有联网青蛙状态 */
	public resetAllNetForgs() {
		var frog: control.frog = null;
		for (var e of this.frogListByClient) {
			e.reset();


		}
	}

	private tmpFrogCounterPoint = new egret.Point(0, 0);
	/**青蛙反作用力 */
	public frogCounterforce(data) {
		let userid = data.account;
		this.tmpFrogCounterPoint.setTo(data.x, data.y);
		for (var frog of this.frogListByClient) {
			if (frog.userid == userid) {

				if (frog.userid == DataProl.account) {
					frog.flicktween(this.tmpFrogCounterPoint);
				}
				else {
					frog.flicktween(this.tmpFrogCounterPoint);
				}

			}
		}
	}
	/**青蛙碰撞反作用力 */
	public frogFlick(data) {
		let userid = data.account;
		let Counterforce_point = new egret.Point(data.x, data.y);
		for (var frog of this.frogListByClient) {
			if (frog.userid == userid) {
				frog.flicktween(Counterforce_point);
			}
		}
	}

	/**获取连线玩家Id*/
	public getLigatureUserId(userID: any) {
		var allLine = this.m_lineMgr.nowServerAllLine;
		var returnId = null;
		if (allLine == null)
			return;
		for (var t = 0; t < allLine.length; t++) {
			if (userID == allLine[t].userOneId)
				returnId = allLine[t].userTwoId;
			if (userID == allLine[t].userTwoId)
				returnId = allLine[t].userOneId;
		}
		if (returnId != null)
			return returnId;
	}
	/**获取我自己 */
	public getMyFrog(userid, name, x, y, frogType, levle, dressup) {
		this.frogListByClient.forEach(e => {
			if (e.userid == userid) {
				e.reset();
				e.born(userid, name, x, y, frogType, levle, dressup);
				this.gameHandle.myUser.userSprit = e;
				return;
			}
		});
		this.createUserFrog(userid, name, x, y, frogType, levle, dressup);
		this.frogListByClient.forEach(e => {
			if (e.userid == userid) {
				this.gameHandle.myUser.userSprit = e;
			}
		});
	}
	/**通过ID查找青蛙 */
	public findFrogById(userid: any) {
		var frog: control.frog = null;
		for (var e of this.frogListByClient) {
			if (e.userid == userid && e.isUsed) {
				frog = e;
				break;
			}
		}
		return frog;
	}
	/**缓存池获取一只青蛙 */
	public getOneFrog() {
		for (var frog of this.frogListByClient) {
			if (frog.isUsed == false) {
				frog.reset();
				return frog;
			}
		}
		return null;

	}

	/**更新青蛙转向 */
	public updateVeer() {
		for (var frog of this.frogListByClient) {
			if (frog.isUsed != false) {
				frog.frogVeer();
			}
		}
	}
	/**青蛙伸舌 */
	public frogLoll() {
		for (var frog of this.frogListByClient) {
			if (frog.userid == DataProl.account) {
				if (frog.frogIsHadLine) {
					return;
				} else {
					frog.loll();
				}
			}
		}
	}
	/**广播伸舌 */
	public showShoot(user) {
		for (var frog of this.frogListByClient) {
			if (frog.userid == user.account) {
				frog.loll();
			}
		}
	}
	/**广播舔到道具动画 */
	public tweenshoot(user) {
		for (var frog of this.frogListByClient) {
			if (frog.userid == user.account) {
				if (user.shootObjStates == 1) {
					if (user.x != null && user.y != null) {
						//广播舔到物品动画
						frog.lickProp(user.x, user.y);
					}
				}
			}
		}
	}
	/**检测玩家是否连线 */
	public checkPlayerIsLine() {
		for (var frog of this.frogListByClient) {
			if (frog.userid == DataProl.account) {
				if (frog.frogIsHadLine) {
					return true;
				} else {
					return false;
				}
			}
		}
		return false;

	}
	// /**检测玩家是否可以连线 */
	// public checkPlayerCanLine() {
	// 	let player;
	// 	//找到自己的青蛙
	// 	for (var frog of this.frogListByClient) {
	// 		if (frog.userid == DataProl.account) {
	// 			if (!frog.frogIsHadLine) {
	// 				player = frog;
	// 			}
	// 		}
	// 	}
	// 	if (player == null || player == undefined) {
	// 		return false;
	// 	}
	// 	for (var frog of this.frogListByClient) {
	// 		if (!frog.frogIsHadLine && frog.type == player.type && frog.userid != DataProl.account) {
	// 			let distance = egret.Point.distance(new egret.Point(player.x, player.y), new egret.Point(frog.x, frog.y));
	// 			if (distance <= (player.frogLineLength + frog.frogLineLength)) {
	// 				return true;
	// 			}
	// 		}
	// 	}
	// 	return false;
	// }

	/**创建玩家青蛙 */
	public createUserFrog(userid, name, x, y, frogType, levle, dressup) {
		var forg = this.getOneFrog();
		if (forg != null) {
			forg.born(userid, name, x, y, frogType, levle, dressup);
			//console.log("重置青蛙成功" + userid + ".." + x);
		}
	}

	/**创建AI属于的青蛙 */
	public createAIFrog(userid, name, x, y, frogType, levle) {
		var forg = this.getOneFrog();
		if (forg != null) {
			forg.born(userid, name, x, y, frogType, levle, DataProl.userCurrentDressUp);
			console.log("重置青蛙成功" + userid + ".." + x);
		}
		return forg;
	}
	/**
	 * 处理房间玩家消息
	 * 1.判断本地有的，从data中清楚，让他们继续移动
	 * 2.本地没有的新建
	 * 3.本地有服务器没有删除（未完）
	 */
	public disposeRoomUserMessage(userData: any) {
		for (var t = 0; t < userData.length; t++) {
			/**一个玩家的信息 */
			var e = userData[t];
			if (e.death.isDeath && e.userId != DataProl.account) {
				let deathFrog = this.findFrogById(e.userId);
				if (deathFrog != null) {
					deathFrog.frogDied();
				}
				continue;
			}
			//如果是自己
			if (e.userId == DataProl.account) {
				this.gameHandle.displaMyInfo(e);
			}
			/**判断用户是否存在 */
			var isHad = this.checkUesdFrogs(e);
			//如果不存在新建,存在继续移动
			if (!isHad) {
				if (e.userId != DataProl.account) {
					this.createAIFrog(e.userId, e.name, e.x, e.y, e.frogType, e.level);
				}
				else {
					this.createUserFrog(e.userId, DataProl.login_data.nickName, e.x, e.y, e.frogType, e.level, e.dressup);
					serverConfig.config.SingleMobile = 1;
				}
			} else {
				if (serverConfig.config.shadowFrog == 1 && e.userId == DataProl.account) {
					serverConfig.config.shadowFrog = 0;
					let shadowFrog = this.findFrogById(DataProl.shadowFrogId);
					if (shadowFrog != null) {
						shadowFrog.x = e.x;
						shadowFrog.y = e.y;
						shadowFrog.targetPoint.x = e.x;
						shadowFrog.targetPoint.y = e.y;
					}
				}
			}
		}
		this.checkUserShow(userData);

	}
	/**检测用户是否在显示范围 */
	private checkUserShow(userData) {
		for (let i = 0; i < this.frogListByClient.length; i++) {
			let isShow = false;
			for (let j = 0; j < userData.length; j++) {
				if (webNetworkMananger.isServerMove) {
					if (this.frogListByClient[i].userid == userData[j].userId && !userData[j].Death.isDeath) {
						isShow = true;
						break;
					}
				} else {
					if (this.frogListByClient[i].userid == userData[j].userId && !userData[j].death.isDeath) {
						isShow = true;
						this.frogListByClient[i].setInRange(userData[j].isInRange);
						break;
					}

				}
			}
			if (!isShow) {
				if (this.frogListByClient[i].userid == DataProl.shadowFrogId) {
					continue;
				}
				//this.frogListByClient[i].frogDied();
			}


		}

	}
	/**检测用户是否存在地图上 */
	private checkUesdFrogs(e: any) {
		for (var i = 0; i < this.frogListByClient.length; i++) {
			//跳过死青蛙
			if (this.frogListByClient[i].userid == -1 || this.frogListByClient[i].isUsed == false)
				continue;
			//如果存在调其updata方法
			if (this.frogListByClient[i].userid == e.userId) {
				this.frogListByClient[i].dataUpdata(e);
				return true;
			}
		}
		serverConfig.config.SingleMobile = 1;
		return false;

	}
}